//
//  MovielistViewController.m
//  豆瓣电影app
//
//  Created by 洛洛大人 on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "MovielistViewController.h"
#import "MovieModel.h"
#import "MovieTableViewCell.h"
#import "LlViewController.h"
@interface MovielistViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)NSMutableArray *movieArray;

@property(nonatomic, retain)UITableView *tableView;


@end

@implementation MovielistViewController
-(void)dealloc{
    [_movieArray release];
    [_tableView release];
    [super dealloc];

    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    [self handleData];
    [self creatTableView];

    
}


-(void)config {
    
    self.movieArray = [NSMutableArray array];
    
    self.navigationItem.title = @"电影";
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.41 green:0.61 blue:0.68 alpha:1];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
}

-(void)rightAction:(UIBarButtonItem *)item{
    
    LlViewController *movieListvc = [[LlViewController alloc]init];
    [self.navigationController pushViewController:movieListvc animated:YES];
    [movieListvc release];
}



-(void)handleData {
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"movielist" ofType:@"txt"];
    
    NSData *data = [NSData dataWithContentsOfFile:path];
    
    NSError *error = nil;
    
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
    
    NSArray *array = [dic objectForKey:@"result"];
    
    for (NSDictionary *dic1 in array) {
        MovieModel *movie = [[MovieModel alloc]init];
        [movie setValuesForKeysWithDictionary:dic1];
        [self.movieArray addObject:movie];
    }
  

    
}


-(void)creatTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - 115) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 180;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"cell"];
    
   
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.cellModel = [self.movieArray objectAtIndex:indexPath.row];
    
    
    return cell;

    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.movieArray.count;
}















@end
